<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category UserRepository.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/3/3  18:14
 * +--------------------------------------------------------+
 */

namespace App\Repositories;

use App\Models\Admin;
use Illuminate\Support\Facades\DB;

class AdminRepository extends BaseRepository
{
    protected $admin;

    public function __construct(Admin $admin)
    {
        $this->admin = $admin;
    }

    public function getInfo()
    {
    }

    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/11  14:06
     */
    public function getValue($id)
    {
        return $this->admin->query()->find($id);
    }

    /**
     * @param $params
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/25  17:05
     */
    public function getList($params)
    {
        $query = $this->admin->query();

        $perPage = $this->perPage($params);
        [$sort, $order] = $this->paramsSet($params);

        $query = $query->select('admins.id', 'admins.name', 'admins.email', 'admins.mobile', DB::raw(' GROUP_CONCAT(la_roles.id) as role_ids'), DB::raw(' GROUP_CONCAT(la_roles.name) as role_names'), 'admins.status', 'admins.created_at', 'admins.updated_at')
            ->leftJoin('model_has_roles', 'model_has_roles.model_id', '=', 'admins.id')
            ->leftJoin('roles', 'model_has_roles.role_id', '=', 'roles.id');
        //
        if (!empty($params['name'])) {
            $query = $query->where('admins.name', 'like', '%' . $params['name'] . '%');
        }
        if (!empty($params['email'])) {
            $query = $query->where('admins.email', 'like', '%' . $params['email'] . '%');
        }
        if (isset($params['status']) && is_numeric($params['status'])) {
            $query = $query->where('admins.status', $params['status']);
        }
        if (!empty($params['role_ids']) && count($params['role_ids'])) {
            $roleIds = implode('|', $params['role_ids']);
            $query   = $query->havingRaw("CONCAT (',',role_ids,',') REGEXP ',({$roleIds}),'");
        }
        if (!empty($params['start_at'])) {
            $query = $query->whereBetween('admins.created_at', [$params['start_at'], $params['end_at']]);
        }

        $list = $query->orderBy($sort, $order)->groupBy('admins.id')->paginate($perPage);

        return $list;
    }

    /**
     * @param $params
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
     * @Depiction 创建用户
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/25  17:47
     */
    public function create($params)
    {
        return $this->admin->query()->create($params);
    }

    /**
     * @param array $params
     * @return bool|int
     * @Depiction修改用户
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/25  17:47
     */
    public function update(array $params)
    {
        $user = $this->admin->query()->find($params['id']);

        unset($user['id']);

        return $user->update($params);
    }


}
